package com.cinyi.cyds.base.mongo;

import java.sql.Timestamp;
import java.util.Date;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cinyi.cyds.util.StringUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * mongo.group(key, cond, initial, reduce,finalize)<br>
 * {"db":"message","key":"topic,fromName","cond":[{"item":"count,$gte,int:0"}],"init":"count:0,total:0","reduce":"aa","finalize":"bb"}
 * @author penghui
 *
 */
public class MongoGroupModel implements java.io.Serializable{
	public static final String TAG_DB_NAME = "db" ;
	public static final String TAG_KEY = "key" ;
	public static final String TAG_COND = "cond" ;
	public static final String TAG_INIT = "init" ;
	public static final String TAG_REDUCE = "reduce" ;
	public static final String TAG_FINALIZE = "finalize" ;
	
	
	private String dbName  = null ;
	private String key  = null ;
	private String cond  = null ;
	private String initial  = null ;
	private String reduce  = null ;
	private String finalize = null  ;
	
	private DBObject keyDbObject = null ;
	private DBObject condDbObject = null ;
	private DBObject initialDbObject = null ;
	
	public MongoGroupModel(){}
	
	public MongoGroupModel(String jsonStr){
		JSONObject json = JSONObject.parseObject(jsonStr);
		dbName = json.getString(TAG_DB_NAME);
		key = json.getString(TAG_KEY);
		buildKeys();
		
		cond = json.getString(TAG_COND);
		buildCond();
		
		initial = json.getString(TAG_INIT);
		buildInitial();
		
		reduce = json.getString(TAG_REDUCE);
		finalize = json.getString(TAG_FINALIZE);
		
	}

	/**
	 * {"init":"count:0,total:0"}
	 */
	private void buildInitial() {
		initialDbObject = new BasicDBObject();
		String[] initArr = initial.split(",");
		for(String s : initArr ){
			String[] initVals = s.split(":");
			initialDbObject.put(initVals[0], StringUtil.getInt(initVals[1],0));
		}
		
	}

	/**
	 * {"cond":"count,$gte,int:0"}
	 * @param tagKey
	 * @param cond2
	 */
	private void buildCond() {
		JSONArray jarray = JSONObject.parseArray(cond);
		if(jarray==null||jarray.size()==0){
			return ;
		}
		condDbObject = new BasicDBObject();
		int size = jarray.size();
		for(int i=0;i<size;i++){
			JSONObject ajson = jarray.getJSONObject(i);
			String[] cond = ajson.getString("item").split(",");
			int pos = cond[2].lastIndexOf(":"); //type:val
			String[] vals = new String[2];
			vals[0]=cond[2].substring(0,pos);
			vals[1]=cond[2].substring(pos+1,cond[2].length());
			if(int.class.getSimpleName().equalsIgnoreCase(vals[0]) || 
					Integer.class.getSimpleName().equalsIgnoreCase(vals[0]) || 
					Long.class.getSimpleName().equalsIgnoreCase(vals[0]) ){
				condDbObject.put(cond[0], new BasicDBObject(cond[1], StringUtil.getLong(vals[1], 0)));
			}else if(Date.class.getSimpleName().equalsIgnoreCase(vals[0]) || 
					Timestamp.class.getSimpleName().equalsIgnoreCase(vals[0]) ){
				condDbObject.put(cond[0], new BasicDBObject(cond[1], StringUtil.getLong(vals[1], 0)));
			}else{
				if(vals[1]==null || "null".equalsIgnoreCase(vals[1])){
					condDbObject.put(cond[0], new BasicDBObject(cond[1], null));
				}else{
					condDbObject.put(cond[0], new BasicDBObject(cond[1], vals[1]));
				}
				
			}
			
		}
		
		
	}

	/**
	 * {"key":"topic,fromName"}
	 */
	private void buildKeys() {
		keyDbObject = new BasicDBObject();
		String[] keyArr = key.split(",");
		
/*		String keyObject="";
		for(String str : keyArr){
			keyObject += str+"-";
		}
		keyObject = keyObject.substring(0,keyObject.length()-1);
		keyDbObject.put(keyObject, true);
		*/
		for(String s : keyArr ){
			keyDbObject.put(s, true);
		}
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
		buildKeys();
	}

	public String getCond() {
		return cond;
	}

	public void setCond(String cond) {
		this.cond = cond;
		buildCond();
	}

	public String getInitial() {
		return initial;
	}

	public void setInitial(String initial) {
		this.initial = initial;
		buildInitial();
	}

	public String getReduce() {
		return reduce;
	}

	public void setReduce(String reduce) {
		this.reduce = reduce;
	}

	public String getFinalize() {
		return finalize;
	}

	public void setFinalize(String finalize) {
		this.finalize = finalize;
	}

	public DBObject getKeyDBObject() {
		return keyDbObject;
	}

	public DBObject getCondDBObject() {
		return condDbObject;
	}

	public DBObject getInitialDBObject() {
		return initialDbObject;
	}
	
}
